var app = getApp();
var common = require('../../utils/util.js');
var mallSaleManId;
const {
  $Toast
} = require('../../dist/base/index');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone:"",
    code:"",
    idCard:"",
    customerName:"",
    btnDisable: false,
    btnValue: "获取验证码",
    loginDisabled:false,
    isScanCode:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if(options.mallSaleManId){
      mallSaleManId=options.mallSaleManId;
      this.setData({
        isScanCode:true
      })
    }else{
      mallSaleManId=-1;
      this.setData({
        isScanCode:false
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  phoneInput(e){
    this.setData({
      phone: e.detail.value
    })
  },
  nameInput(e){
    this.setData({
      customerName: e.detail.value
    })
  },
  idcardInput(e){
    this.setData({
      idCard: e.detail.value
    })
  },
  codeInput(e) {
    this.setData({
      code: e.detail.value
    })
  },
  getCode() {
    var that=this;
    if (common.isNull(this.data.phone)) {
      $Toast({
        content: '请输入手机号',
        type: 'warning',
        duration: 2,
      });
    } else {
      wx.request({
        url: app.getUrl() + '/api/customer/sign_in_captcha',
        data: {
          phone: this.data.phone
        },
        method: 'POST',
        header: {
          "Content-Type": "application/x-www-form-urlencoded",
        },
        success(res) {
          if (res && res.statusCode == 200 && res.data && res.data.status == 200) {
            $Toast({
              content: '发送成功',
              type: 'success',
              duration: 2,
            });
            that.setData({
              btnDisable: true
            })
            var i = 30;
            var time = setInterval(function() {
              i = i - 1;
              that.setData({
                btnValue: i + 's'
              })
              if (i == 0) {
                clearInterval(time);
                that.setData({
                  btnDisable: false,
                  btnValue: '获取验证码',
                })
              }
            }, 1000);
          } else {
            $Toast({
              content: res.data.msg,
              type: 'warning',
              duration: 2,
            });
          }
        },
        fail(res) {
          console.log(res)
          $Toast({
            content: '连接服务器失败',
            type: 'error',
            duration: 2,
          });
        }
      })
    }
  },
  isCard(value) {
    if (!/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(value)) {
      return false
    } else {
      return true
    }
  },
  formSubmit() {
    console.log(mallSaleManId)
    var that=this;
    that.setData({
      loginDisabled:true,
    })
    //不是通过扫销售二维码过来的
    if(mallSaleManId==-1){
      wx.request({
        url: app.getUrl() + '/api/customer/sign_in',
        data: {
          loginName: this.data.phone,
          captcha: this.data.code,
          loginType: '01',
        },
        method: 'POST',
        header: {
          "Content-Type": "application/x-www-form-urlencoded",
        },
        success(res) {
          that.setData({
            loginDisabled: false,
          })
          if (res && res.statusCode == 200 && res.data && res.data.status == 200) {
            console.log(res)
            app.globalData.customerId = res.data.obj.customerId || "";
            app.globalData.accessToken = res.data.obj.accessToken || "";
            app.globalData.openId = res.data.obj.openId || "";
            app.globalData.mallSaleMan = res.data.obj.mallSaleMan || {};
            try {
              wx.setStorageSync('customerId', res.data.obj.customerId || "")
              wx.setStorageSync('accessToken', res.data.obj.accessToken || "")
              wx.setStorageSync('openId', res.data.obj.openId || "")
              wx.setStorageSync('mallSaleMan', res.data.obj.mallSaleMan || {})
            } catch (e) {
              wx.setStorageSync('customerId', "")
              wx.setStorageSync('accessToken', "")
              wx.setStorageSync('openId', "")
              wx.setStorageSync('mallSaleMan', {})
            }
            wx.switchTab({
              url: '../vipRoad/vipRoad',
            })
          } else {
            $Toast({
              content: res.data.msg,
              type: 'warning',
              duration: 2,
            });
          }
        },
        fail(res) {
          that.setData({
            loginDisabled: false,
          })
          console.log(res)
          $Toast({
            content: '连接服务器失败',
            type: 'error',
            duration: 2,
          });
        }
      })
    }else{
      if(this.isCard(this.data.idCard)){
        wx.request({
          url: app.getUrl() + '/api/customer/sign_up',
          data: {
            loginName: this.data.phone,
            captcha: this.data.code,
            loginType: '01',
            mallSaleManId:mallSaleManId,
            customerName:this.data.customerName,
            idCard:this.data.idCard,
          },
          method: 'POST',
          header: {
            "Content-Type": "application/x-www-form-urlencoded",
          },
          success(res) {
            that.setData({
              loginDisabled: false,
            })
            if (res && res.statusCode == 200 && res.data && res.data.status == 200) {
              console.log(res)
              app.globalData.customerId = res.data.obj.customerId || "";
              app.globalData.accessToken = res.data.obj.accessToken || "";
              app.globalData.openId = res.data.obj.openId || "";
              app.globalData.mallSaleMan = res.data.obj.mallSaleMan || {};
              try {
                wx.setStorageSync('customerId', res.data.obj.customerId || "")
                wx.setStorageSync('accessToken', res.data.obj.accessToken || "")
                wx.setStorageSync('openId', res.data.obj.openId || "")
                wx.setStorageSync('mallSaleMan', res.data.obj.mallSaleMan || {})
              } catch (e) {
                wx.setStorageSync('customerId', "")
                wx.setStorageSync('accessToken', "")
                wx.setStorageSync('openId', "")
                wx.setStorageSync('mallSaleMan', {})
              }
              wx.switchTab({
                url: '../vipRoad/vipRoad',
              })
            } else {
              $Toast({
                content: res.data.msg,
                type: 'warning',
                duration: 2,
              });
            }
          },
          fail(res) {
            that.setData({
              loginDisabled: false,
            })
            console.log(res)
            $Toast({
              content: '连接服务器失败',
              type: 'error',
              duration: 2,
            });
          }
        })
      }else{
        wx.showToast({
          title: '请输入正确的身份证号码',
          icon:'none'
        })
        that.setData({
          loginDisabled: false,
        })
      }
    }
  }
})